package typetcp

import (
	"log"
	"net"
	"strings"
	"time"

	"golang.org/x/crypto/ssh"
)

type Ssh string

// protcol://ip:port:user:pwd

func (sshstr Ssh) Brute() bool {
	auth, err := ParseStringToBrute(string(sshstr))
	if err != nil {
		log.Println(err)
		return false
	}
	// fs := strings.SplitN(string(sshstr), "@", 2)
	// if len(fs) != 2 {
	// 	log.Fatal("must |  protcol://ip:port@user:pwd")
	// }
	// afs := strings.SplitN(fs[1], ":", 2)
	// if len(afs) != 2 {
	// 	log.Fatal("must |  user:pwd")
	// }
	// addr := strings.SplitN(fs[0], "://", 2)[1]
	sshConfig := &ssh.ClientConfig{
		User: auth.User,
		Auth: []ssh.AuthMethod{
			ssh.Password(auth.Pwd),
		},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
		Timeout: time.Second * time.Duration(DEFAULT_TIMEOUT),
	}
	if !strings.Contains(auth.Target, ":") {
		auth.Target += ":22"
	}
	_, err = ssh.Dial("tcp", auth.Target, sshConfig)
	if err != nil {
		// log.Println(fmt.Errorf("Failed to dial: %s", err))
		return false
	}
	return true
}
